/**
 * Copyright &copy; 2015-2017  All rights reserved.
 * Licensed under the 深圳市闲闲科技 License, Version 1.0 (the "License");
 *
 */
package com.kehutong.admin.entity;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.coraframework.json.annotation.JSONField;
import org.coraframework.orm.Entity;
import org.coraframework.orm.annotation.Column;
import org.coraframework.orm.annotation.Table;
import org.coraframework.util.Objects;

import com.kehutong.common.entity.Root;

@Table("sys_user")
public class User extends Entity {

	private static final long serialVersionUID = 1L;

	protected User(String id) {
		super(id);
	}

	@Column(comment = "公司编号")
	private String companyNo;
	
	@Column(comment = "公司名称")
	private String companyName;

	@Column(comment = "登录账号")
	private String login_name;
	
	@Column(comment = "当前部门")
	private String department;

	@Column(comment = "登录密码")
	@JSONField(deserialize = false, serialize = false)
	private String password;

	@Column(comment = "员工工号")
	private String no;

	@Column(comment = "员工姓名")
	private String name;

	@Column(comment = "电子邮箱")
	private String email;

	@Column(comment = "办公电话")
	private String phone;

	@Column(comment = "手机号码")
	private String mobile;

	@Column(comment = "用户类型")
	private String user_type;

	@Column(comment = "备注")
	private String remark;

	@Column(comment = "最后登陆IP")
	private String login_ip;

	@Column(comment = "最后登陆日期")
	private LocalDateTime login_date;

	@Column(length = Short.MAX_VALUE, comment = "拥有角色")
	private Set<String> roles;
	
	@Column(comment = "逻辑删除 0: 删除; 1:正常")
	private boolean deleted;
	

	public String getCompanyNo() {
		return companyNo;
	}

	public void setCompanyNo(String companyNo) {
		this.companyNo = companyNo;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	/**
	 * @return the login_name
	 */
	public String getLogin_name() {
		return login_name;
	}

	/**
	 * @param login_name the login_name to set
	 */
	public void setLogin_name(String login_name) {
		this.login_name = login_name;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the no
	 */
	public String getNo() {
		return no;
	}

	/**
	 * @param no the no to set
	 */
	public void setNo(String no) {
		this.no = no;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}

	/**
	 * @param mobile the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	/**
	 * @return the user_type
	 */
	public String getUser_type() {
		return user_type;
	}

	/**
	 * @param user_type the user_type to set
	 */
	public void setUser_type(String user_type) {
		this.user_type = user_type;
	}

	/**
	 * @return the login_ip
	 */
	public String getLogin_ip() {
		return login_ip;
	}

	/**
	 * @param login_ip the login_ip to set
	 */
	public void setLogin_ip(String login_ip) {
		this.login_ip = login_ip;
	}

	/**
	 * @return the login_date
	 */
	public LocalDateTime getLogin_date() {
		return login_date;
	}

	/**
	 * @param login_date the login_date to set
	 */
	public void setLogin_date(LocalDateTime login_date) {
		this.login_date = login_date;
	}

	@JSONField(serialize=false)
	public boolean isAdmin() {
		return Root.isRoot(getId());
	}
	
	@JSONField(serialize=false)
	public boolean isAdminRole() {
		if (isAdmin()) {
			return true;
		}
		
		if (Objects.isEmpty(roles)) {
			return false;
		}
		
		for (String role : roles) {
			if (Root.isRoot(role)) {
				return true;
			}
		}
		
		return false;
	}

	public Set<String> getRoles() {
		if (roles == null) {
			return Collections.emptySet();
		}
		return roles;
	}

	public void setRoles(Set<String> roles) {
		this.roles = roles;
	}
	
	public void removeRole(String roleNo) {
		if (Objects.nonEmpty(roles)) {
			this.roles.remove(roleNo);
		}
	}

	public void addRole(String roleNo) {
		if (Objects.isEmpty(roles)) {
			this.roles = new HashSet<>();
		}
		this.roles.add(roleNo);
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}
	
	public static String toLoginName(String corpid, String userId) {
		return corpid + "_" + userId;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}
	
}